<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <!--<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>
        $.ajax({
            type:'post',
            url:'hellodoPost',
            success:function(body){
                console.log(body);
            },
        });
    </script>-->



    <!--以下是在前端中构建post请求：body为json格式。然后在后端通过Jackson来读取body中的数据-->
    <input type="text" id="userId">
    <input type="text" id="classId">
    <input type="button" value="提交" id="submit">

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

    <script>
        let userIdInput = document.querySelector('#userId');
        let classIdInput = document.querySelector('#classId');
        let button = document.querySelector('#submit');

        button.onclick = function(){
            $.ajax({
                type:'post',
                url:'postJson',
                contentType: 'application/json',
                //post这样的请求，ajax允许用data来构造请求的body部分（此时构造的其实是一个js对象，通过JSON.stringfy将这个js对象转换成一个json格式的字符串）
                data:JSON.stringify({          
                    userId:userIdInput.value,
                    classId:classIdInput.value
                }),
                success:function(body){
                    console.log(body);
                }
            })
        }
    </script>
</body>
</html>